/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package app_99;

import java.sql.*;
import java.util.ArrayList;

/**
 *
 * @author
 */
public class Utils {

    private static Connection conn = null;

    public static boolean doLogin(String est, String pin) {
        try {
            conn = ConnectionPool.getInstance().get();

            PreparedStatement pstmt = conn.prepareStatement("SELECT pin FROM mvpgatewayusers WHERE establishment = ?");

            pstmt.setString(1, est);

            ResultSet rs = pstmt.executeQuery();
            System.out.println("ran query...");

            if (rs.next()) {
                System.out.println(rs.getString("pin"));
                String real_pin = rs.getString("pin");
                if (real_pin.equals(pin)) {
                    ConnectionPool.getInstance().release(conn);
                    return true;
                }
            }

            ConnectionPool.getInstance().release(conn);
            return false;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return true;
    }

    public static ArrayList<String> getEstablishments() {
        ArrayList<String> establishments = new ArrayList<String>();
        
        try {
            conn = ConnectionPool.getInstance().get();

            PreparedStatement pstmt = conn.prepareStatement("SELECT establishment FROM mvpgatewayusers");

            ResultSet rs = pstmt.executeQuery();
            System.out.println("ran query...");

            while (rs.next()) {
                System.out.println(rs.getString("establishment"));
                String estName = rs.getString("establishment");
                establishments.add(estName);
            }

            ConnectionPool.getInstance().release(conn);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return establishments;
    }
}
